/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.cg.objetoCena;

import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import objectreader.JWavefrontModel;

/**
 *
 * @author mac
 */
public class Trator extends JWavefrontModel {

    public Trator() throws IOException {
        super((new File("./data/tractor/tractor.obj")));
    }

    @Override
    public void draw(GLAutoDrawable gLAutoDrawable) {
        GL gl = gLAutoDrawable.getGL();

        angleIncrement += speed;

        if (compileRodaDianteiraEsq != -1) {

            gl.glPushMatrix(); //armazena a matriz corrente
            gl.glTranslatef(0f, chao_y, 0f);
            gl.glTranslatef(0.4f, -0.368f, 0.648f);

            gl.glRotatef(steerAngle, 0, 1, 0);
            gl.glRotatef(angleIncrement, 1, 0, 0);

            gl.glTranslatef(-0.4f, 0.368f, -0.648f);
            gl.glCallList(compileRodaDianteiraEsq);
            gl.glPopMatrix(); //restaura a matriz anterior

        } else {
            Logger.getLogger(Trator.class.getName()).log(Level.SEVERE,
                    "draw() error: method compile() should be previously "
                    + "called to create the proper disply list compileRodasDianteiras.");
        }

        if (compileRodaDianteiraDir != -1) {

            gl.glPushMatrix(); //armazena a matriz corrente
            gl.glTranslatef(0f, chao_y, 0f);
            gl.glTranslatef(-0.4f, -0.368f, 0.648f);

            gl.glRotatef(steerAngle, 0, 1, 0);
            gl.glRotatef(angleIncrement, 1, 0, 0);

            gl.glTranslatef(0.4f, 0.368f, -0.648f);
            gl.glCallList(compileRodaDianteiraDir);
            gl.glPopMatrix(); //restaura a matriz anterior

        } else {
            Logger.getLogger(Trator.class.getName()).log(Level.SEVERE,
                    "draw() error: method compile() should be previously "
                    + "called to create the proper disply list compileRodasDianteiras.");
        }

        if (compileRodasTraseiras != -1) {

            gl.glPushMatrix(); //armazena a matriz corrente
            gl.glTranslatef(0f, chao_y, 0f);
            gl.glTranslatef(0, -0.286f, -0.377f);
            gl.glRotatef(angleIncrement, 1, 0, 0);
            gl.glTranslatef(0, 0.286f, 0.377f);
            gl.glCallList(compileRodasTraseiras);
            gl.glPopMatrix(); //restaura a matriz anterior

        } else {
            Logger.getLogger(Trator.class.getName()).log(Level.SEVERE,
                    "draw() error: method compile() should be previously "
                    + "called to create the proper disply list compileRodasTraseiras.");
        }
        gl.glPushMatrix(); //armazena a matriz corrente
        gl.glTranslatef(0f, chao_y, 0f);
        super.draw(gLAutoDrawable);
        gl.glPopMatrix(); //restaura a matriz anterior
    }

    public void compileParts(GLAutoDrawable gLAutoDrawable, int mode) {
        GL gl = gLAutoDrawable.getGL();

        // eixoTraseiro pneuTraseiro
        this.parts = "rodaDianteiraEsq pneuDiateiroEsq".split(" ");

        compileRodaDianteiraEsq = gl.glGenLists(1);
        gl.glNewList(compileRodaDianteiraEsq, GL.GL_COMPILE);

        drawParts(gLAutoDrawable, mode, compileRodaDianteiraEsq);
        gl.glEndList();

        this.parts = "rodaDianteiraDir pneuDianteiroDir".split(" ");

        compileRodaDianteiraDir = gl.glGenLists(1);
        gl.glNewList(compileRodaDianteiraDir, GL.GL_COMPILE);

        drawParts(gLAutoDrawable, mode, compileRodaDianteiraDir);
        gl.glEndList();

        this.parts = "eixoTraseiro pneuTraseiro".split(" ");

        compileRodasTraseiras = gl.glGenLists(1);
        gl.glNewList(compileRodasTraseiras, GL.GL_COMPILE);

        drawParts(gLAutoDrawable, mode, compileRodasTraseiras);
        gl.glEndList();

        this.compile(gLAutoDrawable, mode);
    }

    public float getSpeed() {
        return speed;
    }

    public void addSpeed(float speed) {
        this.speed += speed;
        if (this.speed > 10.0f) {
            this.speed = 10.0f;
        }
        if (this.speed < -10.0f) {
            this.speed = -10.0f;
        }
    }

    public void addSteerAngle(float steerAngle) {
        this.steerAngle += steerAngle;
        if (this.steerAngle > 40.0f) {
            this.steerAngle = 40.0f;
        }
        if (this.steerAngle < -40.0f) {
            this.steerAngle = -40.0f;
        }
    }
    private int compileRodaDianteiraEsq;
    private int compileRodaDianteiraDir;
    private int compileRodasTraseiras;
    private float chao_y = 0.72f;
    private float speed = 0.0f;
    private float angleIncrement = 0.0f;
    private float steerAngle = 0.0f;
}